package com.example.ujsfish.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.example.ujsfish.R;
import com.example.ujsfish.model.Coordinate;
import com.example.ujsfish.model.MyPoint;
import com.example.ujsfish.model.ship.CurrentPlanPath;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MyView extends View  {
    private static final String TAG = "MyView";
    private float horizontalMove = 0f;
    private float verticalMove = 0f;
    private int k = 30; //坐标变换系数
    public MyView(Context context) {
        this(context,null);

    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint1.setColor(ContextCompat.getColor(context, R.color.colorGreen));//颜色
        mPaint1.setAntiAlias(true);//抗锯齿
        mPaint1.setStyle(Paint.Style.STROKE);//设置填充方式
        mPaint1.setStrokeWidth(6);//描边宽度
        mPaint1.setTextSize(100);

        mPaint2.setColor(ContextCompat.getColor(context, R.color.colorBlack));//颜色
        mPaint2.setAntiAlias(true);//抗锯齿
        mPaint2.setStyle(Paint.Style.STROKE);//设置填充方式
        mPaint2.setStrokeWidth(3);//描边宽度
        mPaint2.setTextSize(35);

        mPaint3.setColor(ContextCompat.getColor(context, R.color.colorRed));//颜色
        mPaint3.setAntiAlias(true);//抗锯齿
        mPaint3.setStyle(Paint.Style.STROKE);//设置填充方式
        mPaint3.setStrokeWidth(8);//描边宽度
        mPaint3.setTextSize(40);

        mPaint4.setStrokeWidth(4);
        mPaint4.setStyle(Paint.Style.STROKE);//设置填充方式
        mPaint4.setTextSize(50);
        mPaint4.setAntiAlias(true);//抗锯齿
    }

    //初始化规划好的路径
    double[] pointX = {-54,-510,-351,315,141,-405,-273,225,-21};
    double[] pointY = {-258,-204,-1089,-894,-141,-282,-1122,-819,-231};

    double[] pointX2 = {50,1000,1000,50,50};
    double[] pointY2 = {50,60,1650,1600,50};



    private CurrentPlanPath currentPlanPath = CurrentPlanPath.getInstance();
    List<MyPoint> pathPointList = currentPlanPath.getPlanPathList();
    List<MyPoint> boundaryPointList = currentPlanPath.getBoundaryList();
    List obsPointList = currentPlanPath.getObstacleList();
    private Coordinate coordinate = Coordinate.getInstance();
    ArrayList<MyPoint> coordinateList = coordinate.getCoordinateList();


    private float mWidth;
    private float mHeight;

    public void setKDown() {
        if (k > 1){
            this.k--;
        }

    }

    public void setKUp() {
        if (k<50){
            this.k++;
        }

    }

    public int getK() {
        return k;
    }

    public void setK(int k) {
        this.k = k;
    }

    public void setHorizontalMoveUp() {
        this.horizontalMove += 10f;
    }

    public void setHorizontalMoveDown() {
        this.horizontalMove -= 10f;
    }

    public void setVerticalMoveUp() {
        this.verticalMove += 10f;
    }

    public void setVerticalMoveDown() {
        this.verticalMove -= 10f;
    }

    public void setHorizontalMove(float horizontalMove) {
        this.horizontalMove = horizontalMove;
    }

    public void setVerticalMove(float verticalMove) {
        this.verticalMove = verticalMove;
    }

    public float getHorizontalMove() {
        return horizontalMove;
    }

    public float getVerticalMove() {
        return verticalMove;
    }

    Path path = new Path();
    Path directionPath = new Path();
    Paint mPaint1 = new Paint();//创建路径画笔
    Paint mPaint2 = new Paint();//创建跟踪点画笔
    Paint mPaint3 = new Paint();//创建跟踪标注点画笔
    Paint mPaint4 = new Paint();//创建方向画笔

    int count = coordinateList.size();

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//         mWidth= getMeasuredWidth();
//         mHeight = getMeasuredHeight();
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画出方向指针
        drawDirection(canvas);

        canvas.translate(mWidth/2 - 150 - horizontalMove,mHeight-200 - verticalMove);//坐标移动到屏幕中央
        //canvas.translate(100,100);//将原点坐标移动到左上方

        //画出边界
        if (boundaryPointList != null && boundaryPointList.size() > 0 ) {
            drawPoolBoundary(canvas,mPaint2);
        }


        //画出障碍点
        if (obsPointList != null && obsPointList.size() > 0) {
            drawObstacle(canvas,mPaint2);
        }


        //将路径规划的点进行变换和绘制
        //ArrayList<MyPoint> myPoints = transformPoint((ArrayList<MyPoint>) pathPointList);

        if (pathPointList != null && pathPointList.size() > 0) {
            drawPlaningPath(canvas);
        }


        //将实时轨迹变换后画出
        if (coordinateList != null && coordinateList.size() > 0) {
            drawCoordinate(canvas);
        }

    }

    private void drawCoordinate(Canvas canvas) {
        ArrayList<MyPoint> list = transformPoint(coordinateList);
        if(list.size() != 0 ){
            System.out.println(list.get(list.size() - 1).getCurrentX() +"  "+ list.get(list.size() - 1).getCurrentY());
        }
        for (int i = 1; i < coordinateList.size() ; i++) {

            canvas.drawCircle(list.get(i).getCurrentX(),list.get(i).getCurrentY(),2f,mPaint2);
        }

        canvas.drawCircle(list.get(list.size() - 1).getCurrentX(),list.get(list.size() - 1).getCurrentY(),4f,mPaint3);

        Log.d("MyView", "集合大小 " + list.size());
    }

    private void drawObstacle(Canvas canvas, Paint mPaint2) {

        for (Object obj  : obsPointList) {
            Path path = new Path();
            List<MyPoint> myPointList = (List<MyPoint>)obj;
            int i = 0;
            for (MyPoint myPoint : myPointList) {
                if (i == 0){
                    path.moveTo(myPoint.getCurrentX()*k,myPoint.getCurrentY()*k);
                }
                path.lineTo(myPoint.getCurrentX()*k,myPoint.getCurrentY()*k);

                i++;
                if (i == myPointList.size()){
                    path.lineTo(myPointList.get(0).getCurrentX()*k,myPointList.get(0).getCurrentY()*k);
                }
            }
            canvas.drawPath(path,mPaint2);
        }


    }

    /**
     * 绘制规划好的路径
     * @param canvas
     */
    private void drawPlaningPath(Canvas canvas) {
        Path path1 = new Path();
        path.moveTo((float) pointX[0], (float) pointY[0]);

        for (int i = 0; i <pointX.length; i++) {
            path.lineTo((float) pointX[i], ((float) pointY[i]));
        }

        //使用foreach画出轨迹
        if (pathPointList.size() != 0){
            for (MyPoint point:
                 pathPointList) {
                path1.lineTo(point.getCurrentX()*k,point.getCurrentY()*k);
            }
        }

        Log.d(TAG, "drawPlaningPath: " + pathPointList.toString());

        //canvas.drawPath(path, mPaint1);
        canvas.drawPath(path1, mPaint1);
//        for (int i = 0; i < pointX.length - 1; i++) {
//            canvas.drawText(String.valueOf(i+1), (float) (0.5*(pointX[i]+pointX[i+1])), (float) (0.5*(pointY[i]+pointY[i+1])), mPaint2);
//        }

    }

    private void drawDirection(Canvas canvas) {
        canvas.translate(100,100);
        directionPath.lineTo(0,-50);
        directionPath.lineTo(-20,-30);
        directionPath.lineTo(0,-50);
        directionPath.lineTo(20,-30);
        canvas.drawPath(directionPath,mPaint4);
        canvas.drawText("西",30,-10,mPaint4);

    }

    /**
     * 绘制池塘边界
     * @param canvas
     * @param paint
     */
    private void drawPoolBoundary(Canvas canvas,Paint paint){
        Path path = new Path();
        Path path1 = new Path();

        path.moveTo(-mWidth/2-50, -mHeight + 50);
        path.lineTo(mWidth/2-150,-mHeight + 60);
        path.lineTo(mWidth/2-150,-5);
        path.lineTo(-mWidth/2-50, -5);
        path.lineTo(-mWidth/2-50, -mHeight + 50);

        int i = 0;

        for (MyPoint myPoint: boundaryPointList) {
            if (i == 0){
                path1.moveTo(myPoint.getCurrentX()*k, myPoint.getCurrentY()*k);
            }

            path1.lineTo(myPoint.getCurrentX()*k,myPoint.getCurrentY()*k);
            i++;
            if (i == boundaryPointList.size()){
                path1.lineTo(boundaryPointList.get(0).getCurrentX()*k,boundaryPointList.get(0).getCurrentY()*k);
            }
        }

        //path1.lineTo(boundaryPointList.get(0).getCurrentX(),boundaryPointList.get(0).getCurrentY());

        Log.d(TAG, "drawPoolBoundary: " + boundaryPointList.toString());

        //canvas.drawPath(path,paint);
        canvas.drawPath(path1,paint);

    }


    //坐标变换,将输入的平面坐标点转换为android坐标
    private ArrayList<MyPoint> transformPoint(ArrayList<MyPoint> coordinateList) {
        ArrayList<MyPoint> list = new ArrayList<>();
        for (MyPoint point: coordinateList){
            list.add(new MyPoint(point.getCurrentX()*k,point.getCurrentY()*k));
        }
        return list;

    }


//    @Override
//    public void run() {
//        while (true) {
//            try {
//                if(count != coordinateList.size()){
//                    count = coordinateList.size();
//                    postInvalidate();
//                    Log.d("MyView", "提醒刷新 ");
//                }
//                Thread.sleep(900);
//                Log.d("MyView", "线程休眠结束 ");
//
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//        }
//    }
}
